def run_algorithm(outlets, user, green, power)
	   data = []
	   preference = user.last.consumption_preference
	   util_cntrl_lvl = user.last.utility_control_lvl
	   or_lvl = utility.override_lvl
	   #green power factor (GPF)
	   #GPF = (GreenSimulation.where(["logged_at like '%?%'",Time.now.hour]).last/PowerLog.last.power_consumption)
       green_percent = green / power
	
	   for outlet in outlets
	   	 if outlet.user_override == true and time.now > or_time + 5.min
	   	 	outlet.user_override = false
	   	 	o = OutletStatus.find(outlet.id)
			o.update_attribute(:user_override, 0)
		 end
	   	 if outlet.user_override == false
			 if outlet.priority >= util_cntrl_lvl and or_lvl <= outlet.priority
				preference = 7
			 else
				preference = user.last.consumption_preference
			 end
			case preference
			when preference = 1
			 if outlet.priority != 1 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			 elsif outlet.priority == 1 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			 end
			when preference = 2
			  if green_percent >= 0.8 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent >= 1 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.8 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  elsif green_percent < 1 and outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 3
			  if green_percent >= 0.4 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent >= 0.6 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.4 and outlet.priority > 1 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 4
			  if green_percent >= 0.1 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent >= 0.3 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 1)
			  elsif green_percent < 0.1 and outlet.priority > 1 and outlet.status == true
				data << outlet.outlet_id
				o = OutletStatus.find(outlet.id)
				o.update_attribute(:status, 0)
			  end
			when preference = 5
				if outlet.status == false
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				  o.update_attribute(:status, 1)
				end
			when preference = 6
				if outlet.priority < 3 and outlet.status == false
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				  o.update_attribute(:status, 1)
				elsif outlet.priority == 3 and outlet.status == true
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				  o.update_attribute(:status, 0)
				end
			when preference = 7
				if outlet.status == true
				  data << outlet.outlet_id
				  o = OutletStatus.find(outlet.id)
				  o.update_attribute(:status, 0)
				end
			end
		 end
	   end
	    return data
	end
